#pragma once

#include "Connection.hpp"

const int defaultefd = -1;

class Recepter : public Connection
{
public:
    Recepter(int efd = defaultefd) : _efd(efd)
    {
        SetEvent(EPOLLIN | EPOLLET);
    }
    ~Recepter()
    {
    }
    void Sender() override
    {
    }

    void Recver() override
    {
        //回调上层获取sockfd
        _efunc(&eq);
        // 创建普通套接字，交给Reaction管理
        while(!eq.empty())
        {
            int sockfd = eq.back().first;
            InetAddr client = eq.back().second;
            eq.pop_back();

            std::shared_ptr<Connection> Con = std::make_shared<Channel>(sockfd, client);
            Con->SetEvent(EPOLLIN | EPOLLET);
            if (_handler != nullptr)
                Con->RegisterHandler(_handler);
            GetOwner()->AddConnection(Con);
        }
    }
    void Expecter() override
    {
    }

    int GetSockfd() override
    {
        return _efd;
    }

private:
    int _efd;
    std::vector<std::pair<int, InetAddr>> eq;
};